$(document).ready(function () {
    var addOrderValidate = $("#addOrder").validate({
        debug: false,
        focusInvalid: false,
        onkeyup: false,
        groups: {
            group1: "ServiceName",
            group2: "CustomerName",
            group3: "Area Bed"
        },
        errorPlacement: function (error, element) {
            if (element.attr("name") == "ServiceName" || element.attr("name") == "CustomerName"
                || element.attr("name") == "Area" || element.attr("name") == "Bed") {
                error.appendTo(element.parent().parent());
            } else {
                error.appendTo(element.parent());
            }
        },
        submitHandler: function (form) {
            var jsonData = $("#addOrder").serializeObject();
            $.ajax({
                url: "EditOrder",
                method: "POST",
                contentType: "application/json;charset=UTF-8",
                async: true,
                dataType: "json",
                data: JSON.stringify(jsonData),
                timeout: timeout,
                success: function (resp) {
                    alert(resp.Message);
                    if (resp.Result) {
                        select("list");
                        query("keep=true", "QueryOrder");
                    }
                }
            }).fail(function (T, S) {
                alert("浏览器超时未响应，请刷新重试");
            });
            return false;
        },

        rules: {
            ServiceName: {
                required: true
            },
            CustomerName: {
                required: true
            },
            Area: {
                required: true
            },
            Bed: {
                required: true
            },
            Shift: {
                required: true
            },
            Start: {
                required: true
            }
        },
        messages: {
            ServiceName: {
                required: "服务项目不能为空"
            },
            CustomerName: {
                required: "客户姓名不能为空"
            },
            Area: {
                required: "病区不能为空"
            },
            Bed: {
                required: "病床不能为空"
            },
            Shift: {
                required: "班次不能为空"
            },
            Start: {
                required: "开始时间不能为空"
            }
        }

    });
    var editScheduleValidate = $("#EditSchedule").validate({
        debug: false,
        focusInvalid: false,
        onkeyup: false,
        groups: {
            username: "CrewName"
        },
        errorPlacement: function (error, element) {
            if (element.attr("name") == "CrewName") {
                error.appendTo(element.parent().parent());
            } else {
                error.appendTo(element.parent());
            }
        },
        submitHandler: function (form) {
            var jsonData = $("#EditSchedule").serializeForm();
            $.ajax({
                url: "EditSchedule",
                method: "POST",
                contentType: "application/json;charset=UTF-8",
                async: true,
                dataType: "json",
                data: JSON.stringify(jsonData),
                timeout: timeout,
                success: function (rep) {
                    alert(rep.Message);
                    if (rep.Result) {
                        select("list");
                        query("keep=true", "QueryOrder");
                    }
                }
            }).fail(function (T, S) {
                alert("浏览器超时未响应，请刷新重试");
            });
            return false;
        },

        rules: {
            CrewName: {
                required: true
            },
            Start: {
                required: true
            },
            End: {
                required: true
            },
            Shift: {
                required: true
            },
            PaymentId: {
                required: true
            },
            Ratio: {
                required: true
            },
            Pay: {
                required: true
            }
        },
        messages: {
            CrewName: {
                required: "服务人员不能为空"
            },
            Start: {
                required: "服务时间不能为空"
            },
            End: {
                required: "结束时间不能为空"
            },
            Shift: {
                required: "班次不能为空"
            },
            PaymentId: {
                required: "结算标准不能为空"
            },
            Ratio: {
                required: "分成比例不能为空"
            },
            Pay: {
                required: "结算金额不能为空"
            }
        }

    });

    var A = new Date().format("yyyy-mm-dd");
    function F(Q) {
        var R = $("div#list table.list tbody");
        R.empty();
        R.append('<tr><td colspan="20">数据加载中...</td></tr>');
        $.ajax({
            url: "OrderPage",
            dataType: "json",
            data: "page=" + Q,
            timeout: timeout,
            success: function (S) {
                update(S);
            }
        }).fail(function (T, S) {
            alert("浏览器超时未响应，请刷新重试");
        });
    }
    var G = {
        count: 1,
        start: 1,
        display: 20,
        border: true,
        border_color: "#DDD",
        text_color: "#3399D5",
        background_color: "#FFF",
        border_hover_color: "#DDD",
        text_hover_color: "#737373",
        background_hover_color: "#F5F5F5",
        images: false,
        mouse: "press",
        onChange: F
    };
    $("div#lpager").paginate(G);
    /*
    *主列表加载数据
    */
    window.update = function (U) {
        var Q = $("div#list table.list tbody");
        var b = U.Items;
        $("div#list table.list input.selall").prop("checked", false);
        Q.empty();
        if (b.length > 0) {
            for (var X = 0; X < b.length; X++) {
                var T = b[X];
                var a = T.State != 256 ? '</td><td><button data-id="' + T.Id + '" class="schedule btn btn-warning"><i class="fa fa-edit fa-lg"></i> 排班</button></td><td>' : "</td><td></td><td>";
                Q.append('<tr><td class="control"></td><td><input name=ids class="row" type="checkbox" value="' + T.Id + '"/></td><td>' + T.Addr + "</td><td>" + T.CustomerName + "</td><td>" + T.Category + "</td><td>" + T.ServiceName + "</td><td>" + T.ServicePrice + "</td><td>" + $.toSpace(T.Start) + "</td><td>" + $.toSpace(T.End) + "</td><td>" + T.StateName + a + '<button data-id="' + T.Id + '" class="edit btn btn-success"><i class="fa fa-edit fa-lg"></i> 编辑</button></td></tr>');
                var c = '<tr class="detail"><td colspan="14"><table class="detail"><tbody>';
                var W = T.Schedules;
                if (W != null && W.length > 0) {
                    for (var V = 0; V < W.length; V++) {
                        var S = W[V];
                        var Z = S.Adjust || 0;
                        var R = S.Pay || 0;
                        var Y = S.Rate || "";
                        c += '<tr><td class="no">' + (V + 1) + "</td><td><dl><dt>服务人员：</dt><dd>" + S.CrewName + "</dd><dt>服务时间：</dt><dd>" + $.toSpace(S.Start).substr(0, 10) + "</dd></dl></td><td><dl><dt>班次：</dt><dd>" + S.ShiftName + "</dd><dt>分成比例：</dt><dd>" + S.Ratio + "</dd></dl></td><td><dl><dt>调整金额：</dt><dd>" + Z + "</dd><dt>结算金额：</dt><dd>" + R + "</dd></dl></td><td><dl><dt>排班人：</dt><dd>" + S.Operator + "</dd><dt>客户评价：</dt><dd>" + Y + '</dd></dl></td><td class="action"><button data-id="' + S.Id + '" class="reschedule btn btn-warning"><i class="fa fa-edit fa-lg"></i> 重排</button></td><td class="action"><button data-id="' + S.Id + '" class="delschedule btn btn-danger"> <i class="fa fa-trash-o fa-lg"></i> 删除</button></td></tr>';
                    }
                } else {
                    c += "<tr><td>该订单无相应排班信息</td></tr>";
                }
                c += "</tbody></table></td></tr>";
                Q.append(c);
            }
        } else {
            Q.append('<tr><td colspan="20">没有相关记录信息</td></tr>');
        }
        G.start = U.Page;
        G.count = U.Total;
        $("div#lpager").paginate(G);
    };

    function P(Q) {
        $("div#edit input").val("");
        $("div#edit input.Start").val(A + " 08:00");
        $("div#edit select option.default").prop("selected", "selected");
        if (Q) {
            $("div#edit input.Id").val(Q.Id);
            $("div#edit input.OrderUnitId").val(Q.OrderUnitId);
            $("div#edit input.ServiceUnitId").val(Q.ServiceUnitId);
            $("div#edit input.ServiceName").val(Q.ServiceName);
            $("div#edit input.ServicePrice").val(Q.ServicePrice);
            $("div#edit input.CustomerId").val(Q.CustomerId);
            $("div#edit input.ServiceId").val(Q.ServiceId);
            $("div#edit input.CustomerName").val(Q.CustomerName);
            $("div#edit select.Shift").val(Q.Shift);
            $("div#edit select.State").val(Q.State);
            $("div#edit input.Area").val(Q.Area);
            $("div#edit input.Bed").val(Q.Bed);
            $("div#edit input.Addr").val(Q.Addr);
            $("div#edit select.Company").val(Q.Company);
            $("div#edit input.Start").val($.toSpace(Q.Start));
            $("div#edit input.End").val($.toSpace(Q.End));
            $("div#edit input.Num").val(Q.Num);
            $("div#edit input.Holidays").val(Q.Holidays);
            $("div#edit input.Discount").val(Q.Discount);
            $("div#edit input.Adjust").val(Q.Adjust);
            $("div#edit input.Charge").val(Q.Charge);
            $("div#edit input.Invoice").val(Q.Invoice);
            $("div#edit select.ReceiptType").val(Q.ReceiptType);
            $("div#edit input.ReceiptNo").val(Q.ReceiptNo);
            $("div#edit input.BillSigner").val(Q.BillSigner);
            $("div#edit input.Receiver").val(Q.Receiver);
            $("div#edit input.Prepay").val(Q.Prepay);
            $("div#edit input.Note").val(Q.Note);
        }
    }
    /**
     * 全班白班只做参考
     */
    $("div#edit input.Start,div#edit input.End").change(function () {
        //修正开始时间
        if (!$.isNullOrEmpty($("div#edit input.Start").val())) {
            var startTime = new Date($("div#edit input.Start").val().replace(/-/g, "/"));
            var newStartTime = new Date((startTime.getFullYear() + "-" + (startTime.getMonth() + 1) + "-" + startTime.getUTCDate() + " 08:00").replace(/-/g, "/"));
            var hour = startTime.getHours();
            if (hour > 12 && hour <= 23) {
                newStartTime = new Date((newStartTime.getFullYear() + "-" + (newStartTime.getMonth() + 1) + "-" + newStartTime.getUTCDate() + " 20:00").replace(/-/g, "/"));
            }
            $("div#edit input.Start").val(newStartTime.format("yyyy-mm-dd HH:MM"));
        }
        //修正结束时间
        if (!$.isNullOrEmpty($("div#edit input.End").val())) {
            var endTime = new Date($("div#edit input.End").val().replace(/-/g, "/"));
            var newEndTime = new Date((endTime.getFullYear() + "-" + (endTime.getMonth() + 1) + "-" + endTime.getUTCDate() + " 08:00").replace(/-/g, "/"));
            var hour = endTime.getHours();
            if (hour > 12 && hour <= 23) {
                newEndTime = new Date((endTime.getFullYear() + "-" + (endTime.getMonth() + 1) + "-" + endTime.getUTCDate() + " 20:00").replace(/-/g, "/"));
            }
            $("div#edit input.End").val(newEndTime.format("yyyy-mm-dd HH:MM"));

            //计算天数
            if (!$.isNullOrEmpty($("div#edit input.Start").val())) {
                var startTime = new Date($("div#edit input.Start").val().replace(/-/g, "/"));
                //var diffTime = (newEndTime - startTime) + 12 * 60 * 60 * 1000; //半天为标准
                var diffTime = newEndTime - startTime;
                var diffDays = parseFloat(diffTime / 1000 / 60 / 60 / 24); //把相差的毫秒数转换为天数
                $("div#edit input.Num").val(diffDays).change();
            }
        } else {
            $("div#edit input.Num").val("");
            $("div#edit input.Holidays").val("").change();
        }
    });

    $("div#list div.query button.query").click(function (Q) {
        query($("div#list div.query form").serialize(), "QueryOrder");
        return false;
    });
    query($("div#list div.query form").serialize(), "QueryOrder");
    $("div#edit .sum").change(function (Q) {
        var servicePrice = parseFloat($("div#edit input.ServicePrice").val());
        //var shift = parseFloat($("div#edit select.Shift").val());
        var workDays = parseFloat($("div#edit input.Num").val());
        var holidays = parseFloat($("div#edit input.Holidays").val()) || 0;
        var discount = parseFloat($("div#edit input.Discount").val()) || 1;
        var adjust = parseFloat($("div#edit input.Adjust").val()) || 0;
        var shiftRadio = 1;//班次不做折算,工作日哪里手动折算
        if (!isNaN(servicePrice) && !isNaN(workDays)) {
            var charge = servicePrice * shiftRadio * (workDays - holidays + 2 * holidays) * discount + adjust;
            $("div#edit input.Charge").val(charge);
        }
    });
    $("ul.tool button.add").click(function (Q) {
        P(null);
        select("edit");
        return false;
    });

    $("button.cancel").click(function (Q) {
        addOrderValidate.resetForm();
        editScheduleValidate.resetForm();
        select("list");
        return false;
    });
    $("ul.tool button.delete").click(function (Q) {
        if (confirm("您确定要删除所有选中的记录吗？")) {
            var R = $("div#list table.list tbody input.row:checked").serialize();
            $.ajax({
                url: "DeleteOrders",
                dataType: "json",
                data: R,
                timeout: timeout,
                success: function (S) {
                    alert(S.Message);
                    if (S.Result) {
                        query("keep=true", "QueryOrder");
                    }
                }
            }).fail(function (T, S) {
                alert("浏览器超时未响应，请刷新重试");
            });
        }
    });
    $("div#list table.list tbody").click(function (Q) {
        var S = $(Q.target);
        if (S.is("button.delschedule")) {
            if (confirm("您确定要删除该记录吗？")) {
                var U = S.data("id");
                $.ajax({
                    url: "DeleteSchedule",
                    dataType: "json",
                    data: "Id=" + U,
                    timeout: timeout,
                    success: function (V) {
                        alert(V.Message);
                        if (V.Result) {
                            query("keep=true", "QueryOrder");
                        }
                    }
                }).fail(function (W, V) {
                    alert("浏览器超时未响应，请刷新重试");
                });
            }
        } else {
            if (S.is("button.edit")) {
                var U = S.data("id");
                $.ajax({
                    url: "GetOrder",
                    dataType: "json",
                    data: "Id=" + U,
                    timeout: timeout,
                    success: function (V) {
                        P(V);
                        select("edit");
                    }
                }).fail(function (W, V) {
                    alert("浏览器超时未响应，请刷新重试");
                });
            } else {
                if (S.is("button.take")) {
                    var U = S.data("id");
                    $.ajax({
                        url: "TakeOrder",
                        dataType: "json",
                        data: "Id=" + U,
                        timeout: timeout,
                        success: function (V) {
                            alert(V.Message);
                            if (V.Result) {
                                query("keep=true", "QueryOrder");
                            }
                        }
                    }).fail(function (W, V) {
                        alert("浏览器超时未响应，请刷新重试");
                    });
                } else {
                    if (S.is("button.dispatch")) {
                        var U = S.data("id");
                        $.ajax({
                            url: "DispatchOrder",
                            dataType: "json",
                            data: "Id=" + U,
                            timeout: timeout,
                            success: function (V) {
                                alert(V.Message);
                                if (V.Result) {
                                    query("keep=true", "QueryOrder");
                                }
                            }
                        }).fail(function (W, V) {
                            alert("浏览器超时未响应，请刷新重试");
                        });
                    } else {
                        if (S.is("button.schedule")) {
                            var U = S.data("id");
                            $.ajax({
                                url: "GetOrder",
                                dataType: "json",
                                data: "Id=" + U,
                                timeout: timeout,
                                success: function (V) {
                                    $("div#schedule input").val("");
                                    $("div#schedule input.OrderId").val(U);
                                    $("div#schedule input.CustomerName").val(V.CustomerName);
                                    $("div#schedule input.ServiceId").val(V.ServiceId);
                                    $("div#schedule input.ServiceName").val(V.ServiceName);
                                    $("div#schedule input.ServicePrice").val(V.ServicePrice);
                                    $("div#schedule input.IsHoliday").prop("checked", false);
                                    $("div#schedule select.Shift").val(0);//默认全天
                                    $("div#schedule select.PaymentId").change();
                                    select("schedule");
                                }
                            }).fail(function (W, V) {
                                alert("浏览器超时未响应，请刷新重试");
                            });
                        } else {
                            if (S.is("button.reschedule")) {
                                var U = S.data("id");
                                $.ajax({
                                    url: "GetSchedule",
                                    dataType: "json",
                                    data: "Id=" + U,
                                    timeout: timeout,
                                    success: function (V) {
                                        $("div#schedule input.Id").val(V.Id);
                                        $("div#schedule input.OrderId").val(V.OrderId);
                                        $("div#schedule input.CustomerName").val(V.CustomerName);
                                        $("div#schedule input.ServiceName").val(V.ServiceName);
                                        $("div#schedule input.ServicePrice").val(V.ServicePrice);
                                        $("div#schedule input.CrewId").val(V.CrewId);
                                        $("div#schedule input.CrewName").val(V.CrewName);
                                        $("div#schedule input.CrewTel").val(V.CrewTel);
                                        $("div#schedule input.Start").val($.toSpace(V.Start));
                                        $("div#schedule input.End").val($.toSpace(V.End));
                                        $("div#schedule select.Shift").val(V.Shift);
                                        $("div#schedule input.IsHoliday").prop("checked", V.IsHoliday);
                                        $("div#schedule select.PaymentId").val(V.PaymentId).change();
                                        $("div#schedule input.Ratio").val(V.Ratio);
                                        $("div#schedule input.Adjust").val(V.Adjust);
                                        $("div#schedule input.Pay").val(V.Pay);
                                        $("div#schedule input.Rate").val(V.Rate);
                                        $("div#schedule input.Note").val(V.Note);
                                        select("schedule");
                                    }
                                }).fail(function (W, V) {
                                    alert("浏览器超时未响应，请刷新重试");
                                });
                            } else {
                                if (S.is("td.control")) {
                                    var R = S.parent("tr");
                                    var T = R.next("tr.detail");
                                    T.toggle();
                                    R.toggleClass("shown");
                                }
                            }
                        }
                    }
                }
            }
        }
    });
    //pay样式控件值改变,计算结算金额
    $("div#schedule .pay,div#schedule input.Start,div#schedule input.End").change(function (R) {
        //时间修正
        checkDateTime();
        //计算金额
        calcPay();
    });
    //结算标准改变
    $("div#schedule select.PaymentId").change(function (R) {
        var Q = $(this).find("option:selected");
        $("div#schedule input.Ratio").val(Q.data("ratio")).change();
    });

    /**
     * 校验时间     
     * @returns {} 
     */
    function checkDateTime() {
        if (!$.isNullOrEmpty($("div#schedule input.Start").val())) {
            var startTime = new Date($("div#schedule input.Start").val().replace(/-/g, "/"));
            var shift = parseFloat($("div#schedule select.Shift").val());
            var hour = shift == 2 ? 20 : 8;
            startTime.setHours(hour);
            $("div#schedule input.Start").val(startTime.format("yyyy-mm-dd HH:MM"));
        }

        if (!$.isNullOrEmpty($("div#schedule input.End").val())) {
            //有值矫正
            var endTime = new Date($("div#schedule input.End").val().replace(/-/g, "/"));
            var newEndTime = new Date((endTime.getFullYear() + "-" + (endTime.getMonth() + 1) + "-" + endTime.getUTCDate() + " 08:00").replace(/-/g, "/"));
            if (shift == 1) { //白班结束时间在晚上8点
                newEndTime = new Date((endTime.getFullYear() + "-" + (endTime.getMonth() + 1) + "-" + endTime.getUTCDate() + " 20:00").replace(/-/g, "/"));
            }
            $("div#schedule input.End").val(newEndTime.format("yyyy-mm-dd HH:MM"));
        } else {
            //没有结束时间根据开始时间赋默认值
            if (!$.isNullOrEmpty($("div#schedule input.Start").val())) {
                var startTime = new Date($("div#schedule input.Start").val().replace(/-/g, "/"));
                var hourDiff = shift > 0 ? 12 : 24;
                var endTime = new Date(startTime.getTime());
                endTime.setHours(startTime.getHours() + hourDiff);
                $("div#schedule input.End").val(endTime.format("yyyy-mm-dd HH:MM"));
            }
        }
    }
    /**
     * 计算金额
     * @returns {} 
     */
    function calcPay() {
        if (!$.isNullOrEmpty($("div#schedule input.Start").val()) && !$.isNullOrEmpty($("div#schedule input.End").val())) {
            var startTime = new Date($("div#schedule input.Start").val().replace(/-/g, "/"));
            var endTime = new Date($("div#schedule input.End").val().replace(/-/g, "/"));
            var diffTime = (endTime - startTime) + 12 * 60 * 60 * 1000;//半天为标准
            var diffDays = parseInt(diffTime / 1000 / 60 / 60 / 24); //把相差的毫秒数转换为天数
            if (diffDays < 0) {
                diffDays = 0;
            }

            //收费标准
            var servicePrice = parseFloat($("div#schedule input.ServicePrice").val());
            //班次
            var shift = parseFloat($("div#schedule select.Shift").val());
            //分成比例
            var ratio = parseFloat($("div#schedule input.Ratio").val());
            //节假日双倍
            var isHoliday = $("div#schedule input.IsHoliday").prop("checked") ? 2 : 1;
            //调整金额
            var adjust = parseFloat($("div#schedule input.Adjust").val()) || 0;
            var shiftRate = shift == "0" ? 1 : 0.5;
            if (!isNaN(servicePrice) && !isNaN(ratio)) {
                $("div#schedule input.Pay").val(servicePrice * ratio * isHoliday * shiftRate * diffDays + adjust);
            }
        }
    }

    var E = {
        count: 1,
        start: 1,
        display: 20,
        border: true,
        border_color: "#DDD",
        text_color: "#3399D5",
        background_color: "#FFF",
        border_hover_color: "#DDD",
        text_hover_color: "#737373",
        background_hover_color: "#F5F5F5",
        images: false,
        mouse: "press",
        onChange: D
    };

    function C(R) {
        var U = $("div#service table.list tbody");
        var S = R.Items;
        U.empty();
        if (S.length > 0) {
            for (var Q = 0; Q < S.length; Q++) {
                var T = S[Q];
                U.append("<tr><td>" + T.Name + "</td><td>" + T.Type + "</td><td>" + T.Unit + "</td><td>" + T.Price + '</td><td><button data-id="' + T.Id + '" data-name="' + T.Name + '" data-price="' + T.Price + '" class="select btn btn-success"><i class="fa fa-check fa-lg"></i> 选 择</button></td></tr>');
            }
        } else {
            U.append('<tr><td colspan="20">没有相关记录信息</td></tr>');
        }
        E.start = R.Page;
        E.count = R.Total;
        $("div#spager").paginate(E);
    }
    function D(Q) {
        var R = $("div#service table.list tbody");
        R.empty();
        R.append('<tr><td colspan="20">数据加载中...</td></tr>');
        $.ajax({
            url: "../Maintain/ServicePage",
            dataType: "json",
            data: "page=" + Q,
            timeout: timeout,
            success: function (S) {
                C(S);
            }
        }).fail(function (T, S) {
            alert("浏览器超时未响应，请刷新重试");
        });
    }
    function L(R, Q) {
        var S = $("div#service table.list tbody");
        S.empty();
        S.append('<tr><td colspan="20">数据加载中...</td></tr>');
        $.ajax({
            url: Q,
            dataType: "json",
            data: R,
            timeout: timeout,
            success: function (T) {
                C(T);
            },
            error: function (U, T, V) {
                alert("浏览器超时未响应，请刷新重试");
            }
        });
    }
    $("div#service div.query button.query").click(function (Q) {
        L($("div#service div.query form").serialize(), "../Maintain/QueryService");
        return false;
    });
    $("div#edit button#ServiceBrowser").click(function (Q) {
        $("div#service").dialog("open");
        $("div#spager").paginate(E);
        L($("div#service div.query form").serialize(), "../Maintain/QueryService");
        return false;
    });
    $("div#service table.list tbody").click(function (Q) {
        var R = $(Q.target);
        if (R.is("button.select")) {
            $("div#edit input.ServiceId").val(R.data("id"));
            $("div#edit input.ServiceName").val(R.data("name"));
            $("div#edit input.ServicePrice").val(R.data("price")).change();
            $("div#service").dialog("close");
        }
    });

    function M(T) {
        var Q = $("div#customer table.list tbody");
        var Z = T.Items;
        Q.empty();
        if (Z.length > 0) {
            for (var V = 0; V < Z.length; V++) {
                var S = Z[V];
                var X = S.Tel || "";
                var Y = S.IdNo || "";
                var U = S.Addr || "";
                var R = S.Contact || "";
                var W = S.ContactTel || "";
                var a = S.ContactRel || "";
                Q.append("<tr><td>" + S.Name + "</td><td>" + X + "</td><td>" + Y + "</td><td>" + U + "</td><td>" + R + "</td><td>" + W + "</td><td>" + a + '</td><td><button data-id="' + S.Id + '" data-name="' + S.Name + '" data-tel="' + X + '" class="select btn btn-success"><i class="fa fa-check fa-lg"></i> 选 择</button></td></tr>');
            }
        } else {
            Q.append('<tr><td colspan="20">没有相关记录信息</td></tr>');
        }
        J.start = T.Page;
        J.count = T.Total;
        $("div#cpager").paginate(J);
    }
    function H(Q) {
        var R = $("div#customer table.list tbody");
        R.empty();
        R.append('<tr><td colspan="20">数据加载中...</td></tr>');
        $.ajax({
            url: "CustomerPage",
            dataType: "json",
            data: "page=" + Q,
            timeout: timeout,
            success: function (S) {
                M(S);
            }
        }).fail(function (T, S) {
            alert("浏览器超时未响应，请刷新重试");
        });
    }
    var J = {
        count: 1,
        start: 1,
        display: 20,
        border: true,
        border_color: "#DDD",
        text_color: "#3399D5",
        background_color: "#FFF",
        border_hover_color: "#DDD",
        text_hover_color: "#737373",
        background_hover_color: "#F5F5F5",
        images: false,
        mouse: "press",
        onChange: H
    };

    function B(R, Q) {
        var S = $("div#customer table.list tbody");
        S.empty();
        S.append('<tr><td colspan="20">数据加载中...</td></tr>');
        $.ajax({
            url: Q,
            dataType: "json",
            data: R,
            timeout: timeout,
            success: function (T) {
                M(T);
            },
            error: function (U, T, V) {
                alert("浏览器超时未响应，请刷新重试");
            }
        });
    }
    $("div#customer div.query button.query").click(function (Q) {
        B($("div#customer div.query form").serialize(), "QueryCustomer");
        return false;
    });
    $("div#edit button#CustomerBrowser").click(function (Q) {
        $("div#customer").dialog("open");
        $("div#cpager").paginate(J);
        B($("div#customer div.query form").serialize(), "QueryCustomer");
        return false;
    });
    $("div#customer table.list tbody").click(function (Q) {
        var T = $(Q.target);
        if (T.is("button.select")) {
            var U = T.data("id");
            var S = T.data("name");
            var R = T.data("tel");
            $("div#edit input.CustomerId").val(U);
            $("div#edit input.CustomerName").val(S);
            $("div#edit input.CustomerTel").val(R);
            $("div#customer").dialog("close");
        }
    });
    var K = {
        count: 1,
        start: 1,
        display: 20,
        border: true,
        border_color: "#DDD",
        text_color: "#3399D5",
        background_color: "#FFF",
        border_hover_color: "#DDD",
        text_hover_color: "#737373",
        background_hover_color: "#F5F5F5",
        images: false,
        mouse: "press",
        onChange: I
    };

    function N(S) {
        var Q = $("div#crew table.list tbody");
        var W = S.Items;
        Q.empty();
        if (W.length > 0) {
            for (var T = 0; T < W.length; T++) {
                var R = W[T];
                var X = R.Skill || "";
                var V = R.IdNo || "";
                var U = R.Tel || "";
                Q.append("<tr><td>" + R.Name + "</td><td>" + R.Gender + "</td><td>" + V + "</td><td>" + R.Type + "</td><td>" + X + "</td><td>" + R.Status + "</td><td>" + R.PaymentName + "</td><td>" + U + "</td><td>" + R.OwnerUnitName + "</td><td>" + R.ServiceUnitName + '</td><td><button data-id="' + R.Id + '" class="select btn btn-success"><i class="fa fa-check fa-lg"></i> 选 择</button></td></tr>');
            }
        } else {
            Q.append('<tr><td colspan="20">没有相关记录信息</td></tr>');
        }
        K.start = S.Page;
        K.count = S.Total;
        $("div#wpager").paginate(K);
    }
    function I(Q) {
        var R = $("div#crew table.list tbody");
        R.empty();
        R.append('<tr><td colspan="20">数据加载中...</td></tr>');
        $.ajax({
            url: "CrewPage",
            dataType: "json",
            data: "page=" + Q,
            timeout: timeout,
            success: function (S) {
                N(S);
            }
        }).fail(function (T, S) {
            alert("浏览器超时未响应，请刷新重试");
        });
    }
    function O(R, Q) {
        var S = $("div#crew table.list tbody");
        S.empty();
        S.append('<tr><td colspan="20">数据加载中...</td></tr>');
        $.ajax({
            url: Q,
            dataType: "json",
            data: R,
            timeout: timeout,
            success: function (T) {
                N(T);
            },
            error: function (U, T, V) {
                alert("浏览器超时未响应，请刷新重试");
            },
        });
    }
    $("div#crew div.query button.query").click(function (Q) {
        O($("div#crew div.query form").serialize(), "QueryCrew");
        return false;
    });
    $("div#schedule button#CrewBrowser").click(function (Q) {
        $("div#crew").dialog("open");
        $("div#wpager").paginate(K);
        O($("div#crew div.query form").serialize(), "QueryCrew");
        return false;
    });
    $("div#crew table.list tbody").click(function (Q) {
        var R = $(Q.target);
        if (R.is("button.select")) {
            var S = R.data("id");
            $.ajax({
                url: "GetCrew",
                dataType: "json",
                data: "Id=" + S,
                timeout: timeout,
                success: function (T) {
                    $("div#schedule input.CrewId").val(T.Id);
                    $("div#schedule input.CrewName").val(T.Name);
                    $("div#schedule input.CrewTel").val(T.Tel);
                    $("div#schedule select.PaymentId").val(T.PaymentId);
                    $("div#schedule input.Ratio").val(T.Ratio).change();
                    if (T.Ratio) {
                        $("div#schedule input.Pay").val(T.Ratio * $("div#schedule input.ServicePrice").val());
                    }
                    $("div#crew").dialog("close");
                }
            }).fail(function (U, T) {
                alert("浏览器超时未响应，请刷新重试");
            });
        }
    });
});